/** ***************************************************************************
 * AppMain.cpp
 * 2012 CoffeeParty Ltd.
 * Date Created: 2012-07-21                                  format YYYY-MM-DD
 * Revision Id: $Id$
 ******************************************************************************
 */

#include "AppMain.hpp"

c_int cp_init()
{
    const SDL_VideoInfo *p_info = NULL;

    try
    {
        if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
            throw("Failed on initialising SDL");

        if (!(p_info = SDL_GetVideoInfo()))
            throw("Failed to query video");

        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, gp_params.ogl_redbit());
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, gp_params.ogl_bluebit());
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, gp_params.ogl_greenbit());
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, gp_params.ogl_alphabit());
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, gp_params.ogl_depthbit());
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, gp_params.ogl_doublebuffer());

        gp_params.bitspp(p_info->vfmt->BitsPerPixel);
        if (SDL_SetVideoMode(gp_params.width(), gp_params.height(),
            gp_params.bitspp(), SDL_OPENGL) == NULL)
            throw("Failed in creating window context");
    }
    catch (c_const_bytep exception)
    {
        c_print("initialiseSDL(): %s", exception);
        return CPRS_FAIL_INIT;
    }

    return CPRS_OK;
}

c_int cp_init_ogl()
{
    gp_cue.clearColor(caerulean_colors[0]);
    gp_cue.clearDepth();
    gp_cue.toOrtho(gp_params.width(), gp_params.height());
    gp_cue.clearAll();
    gp_cue.fixSDLFace();

    return CPRS_OK;
}

c_int cp_event()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
        case SDL_MOUSEMOTION:
            break;
        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
            switch (event.button.button)
            {
            case SDL_BUTTON_LEFT:
                break;
            case SDL_BUTTON_RIGHT:
                break;
            case SDL_BUTTON_MIDDLE:
                break;
            case SDL_BUTTON_WHEELUP:
                break;
            case SDL_BUTTON_WHEELDOWN:
                break;
            }
            break;
        case SDL_KEYDOWN:
        case SDL_KEYUP:
            switch (event.key.keysym.sym)
            {
            case SDLK_ESCAPE:
                if (event.type == SDL_KEYDOWN)
                    g_runstate = true;
                break;
            case SDLK_c:
                if (event.type == SDL_KEYDOWN)
                    createScreenshot();
                break;
            default:
                break;
            }
            break;
        case SDL_QUIT:
            g_runstate = true;
            break;
        default:
            break;
        }
    }

    return CPRS_OK;
}

c_bool cp_runcheck()
{
    return g_runstate;
}

CoffeeTimer *gp_timer = NULL;
SugarImage *gp_tga1 = NULL;
SugarImage *gp_tga2 = NULL;
CoffeeTexture *gp_tex1 = NULL;
CoffeeTexture *gp_tex2 = NULL;
GLuint gp_texid = 0;

c_void cp_init_resource()
{
    gp_timer = new CoffeeTimer();
    gp_timer->initTimer();

    c_string tmp;
    extractFilePath(tmp, g_args);
    c_string asset_img1 = tmp + "Assets/unpic.tga";

    gp_tga1 = new SugarTga(asset_img1.c_str());
    gp_tex1 = new CoffeeTexture(gp_tga1);
    gp_tex2 = new CoffeeTexture(gp_tga1, true);
}

c_void cp_tick()
{
    gp_timer->tick();
}

c_int cp_render()
{
    gp_cue.enable2DRendering();
    gp_tex1->testDrawOGL();
    gp_tex2->testDrawOGL(c_dimension<c_int>(256, 0));
    gp_cue.disable2DRendering();

    return CPRS_OK;
}

c_int cp_loop()
{
    cp_init_resource();
    while (!cp_runcheck())
    {
        cp_tick();
        cp_init_ogl();
        cp_render();

        cp_event();
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return CPRS_OK;
}

c_int cp_cleanup()
{
    safe_drop(gp_tga1);
    safe_drop(gp_tex1);
    safe_drop(gp_tex2);
    safe_drop(gp_timer);

    deleteTexture(&gp_texid);
    c_print("main(): Clean exit");
    return CPRS_OK;
}

c_int main(c_int argc, c_bytepp argv)
{
    if (argc)
        g_args = argv[0];

    cp_init();
    cp_loop();
    cp_cleanup();
    return CPRS_OK;
}

/** ***************************************************************************
 * LAST LEAK SUMMARY: 08-12-2012
 * ==9423==    definitely lost: 2,968 bytes in 12 blocks
 * ==9423==    indirectly lost: 2,296 bytes in 21 blocks
 * ==9423==      possibly lost: 262,144 bytes in 1 blocks
 * ==9423==    still reachable: 91,028 bytes in 1,241 blocks
 * ==9423==         suppressed: 0 bytes in 0 blocks
 * ==9423== Rerun with --leak-check=full to see details of leaked memory
 * ==9423==
 * ==9423== For counts of detected and suppressed errors, rerun with: -v
 * ==9423== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 64 from 14)
 *
 ******************************************************************************
 */

